﻿function getBrowserName()
{
    var browserName = ""; 

    var ua = navigator.userAgent.toLowerCase(); 
    if ( ua.indexOf( "opera" ) != -1 ) { 
        browserName = "opera"; 
    } 
    else if ( ua.indexOf( "msie" ) != -1 ) { 
        browserName = "msie"; 
    } 
    else if ( ua.indexOf( "safari" ) != -1 ) { 
        browserName = "safari"; 
    } 
    else if ( ua.indexOf( "mozilla" ) != -1 ) { 
        if ( ua.indexOf( "firefox" ) != -1 ) { 
            browserName = "firefox"; 
        } else { 
            browserName = "mozilla"; 
        } 
    } 

    return browserName; 
}; 
/*Get elememt top left in document-------------------------------------------*/
function getElementLeft(elem) 
{	    
    xPos = elem.offsetLeft;
        
    tempEl = elem.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getElementTop(elem) 
{	    
    yPos = elem.offsetTop;
    tempEl = elem.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function NoEnterKey(e)
{
    if(window.event == undefined)
    {
        //alert(e.keyCode);
        if (e.keyCode == 13) 
        {
            return false;
        }
    }
    else if(window.event)
    {
        if (window.event.keyCode == 13) 
        {
            event.returnValue = false; 
            event.cancel = true;
        }
    }
}

/*Drag drop--------------------------------------------------------*/        
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
var browserName = "";

var cssClassName;
var mapSize = 512;
var flagVisible = false;
var container;
var containerLeft;
var containerTop;

var necessaryData = "";

function movemouse(e)
{
    if (isdrag)
    {
        var flagNode = document.getElementById("imgFlag");
        var curX = nn6 ? e.clientX : event.clientX;
        var curY = nn6 ? e.clientY : event.clientY;
        var xx = tx + curX - x; 
        var yy = ty + curY - y;
        var dx = 0;
        var dy = 0;
        
        if(cssClassName == "map")
        {   
            if(curX < containerLeft || curY < containerTop 
                || curX > (containerLeft + mapSize) || curY > (containerTop + mapSize))
                return false;
                
            dx = xx - parseInt(dobj.style.left);
            dy = yy - parseInt(dobj.style.top);
            
            dobj.style.left = xx + "px";
            dobj.style.top = yy + "px";
                                
            if(flagVisible == true)
            {    
                flagNode.style.left = (parseInt(flagNode.style.left + 0,10) + dx) + "px";
                flagNode.style.top = (parseInt(flagNode.style.top + 0,10) + dy) + "px";
                var newLeft = parseInt(flagNode.style.left + 0,10);
                var newTop = parseInt(flagNode.style.top + 0,10);                
            }
        }
        
        else
        {
            if(xx > 0 && yy > 0 && xx < (512-flagNode.width) && yy <(512-flagNode.height))
            {
                flagNode.style.left = xx + "px";
                flagNode.style.top = yy + "px";
            }
        }
        
        return false;
    }
}

function selectmouse(e)
{
  var fobj      = nn6 ? e.target : event.srcElement;
  
  if (fobj.className=="dragme" || fobj.className=="map")
  {
    isdrag = true;
    cssClassName = fobj.className;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0,10);
    ty = parseInt(dobj.style.top+0,10);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    
    if(cssClassName=="map")
    {
        if(browserName=="msie")
        {
            isdrag = false;
            return false;
        }
        container = document.getElementById("mapContainer");
        containerLeft = getElementLeft(container);
        containerTop = getElementTop(container);
    }
    
    document.onmousemove=movemouse;
    return false;
  }
}

function deselectmouse(e)
{   
    if(isdrag == true)
    {
        isdrag=false;
        
        var flagNode = document.getElementById("imgFlag");
        var xx, yy;
        if(cssClassName == "dragme")
        {
            xx = parseInt(dobj.style.left+0,10) + flagNode.width/2;
            yy = parseInt(dobj.style.top+0,10) + flagNode.height;
        }
        else //className = "map"
        {
            if(flagVisible==true)
            {
                xx = parseInt(flagNode.style.left+0,10)+ flagNode.width/2 - parseInt(dobj.style.left+0,10);
                yy = parseInt(flagNode.style.top+0,10) + flagNode.height - parseInt(dobj.style.top+0,10);
            }
            
            var curX = nn6 ? e.clientX : event.clientX;
            var curY = nn6 ? e.clientY : event.clientY;
            var centerX = mapSize/2 - (curX - x);
            var centerY = mapSize/2 - (curY - y);
            
            var res = "newcenter=pixel" + "@" + centerX + "@" + centerY;            
            LoadMap(res);            
        }
        
        //Goi Server tinh toa do lat long tu xx, yy la toa do man hinh
        CallServer("PlaceCoor@" + xx + "@" + yy);        
    }
    
}
document.onmousedown=selectmouse;
document.onmouseup=deselectmouse;//new Function("isdrag=false");

/*-----------------------------------------------*/

function getXmlHttpObject()
{
    if(window.XMLHttpRequest)
    {
	    return new XMLHttpRequest();
    }
    if(window.ActiveXObject)
    {
	    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}    

function GetLatLong(request)
{
    xmlHttp = getXmlHttpObject();
    if(xmlHttp == null)
    {
	    alert("Your browser does not support XML HTTP Request!!!");
	    return;
    }
	
    var url = "GetPlaceCoor.aspx?" + request + "&t=" + new Date().getTime();
    xmlHttp.onreadystatechange = handleGetLatLong;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function GetPlacePoint()
{
    xmlHttp = getXmlHttpObject();
    if(xmlHttp == null)
    {
	    alert("Your browser does not support XML HTTP Request!!!");
	    return;
    }
	
    var url = "GetPlacePoint.aspx?" + "&t=" + new Date().getTime();
    xmlHttp.onreadystatechange = handleGetPlacePoint;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function handleGetPlacePoint()
{
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    { 
        var flagNode = document.getElementById("imgFlag");
        var strPlacePoint = xmlHttp.responseText;
        var arrXY = strPlacePoint.split("@@");
        
        if(arrXY.length < 2)
            return;
            
        var newleft = parseInt(arrXY[0]) - 11;
        var newtop = parseInt(arrXY[1]) - 34;
        if(newleft > 0 && newleft< 512 && newtop > 0 && newtop < 512)
        {
            flagNode.style.left = newleft + "px";
            flagNode.style.top = newtop + "px";
            flagNode.style.display = "block";
            flagVisible = true;
        }              
    }
}

function hideFlag()
{
    document.getElementById("imgFlag").style.display = "none";
}





/*Change language ---------------------------------------------------*/
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function ChangeLanguage(language)
{    
    setCookie("language", language, 15);
    document.location.href = document.location;
}